package com.lm.scau.dyb.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lm.scau.dyb.R;
import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.utils.ImageLoaderUtil;
import com.lm.scau.dyb.view.ViewHolder;

import java.util.List;

/**
 * Function:
 * auth：liming on 2016/5/30 13:55
 * mail：liming@wxchina.com
 */
public class LMGridViewAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<String> utlAndTitle;
    private List<Integer> imgs;

    public LMGridViewAdapter(BaseActivity avtivity, List<String> titles, List<Integer> imgs) {
        if (null != imgs) {
            this.imgs = imgs;

        }
        this.utlAndTitle = titles;
        this.layoutInflater = LayoutInflater.from(avtivity);
    }

    @Override
    public int getCount() {
        return utlAndTitle.size();
    }

    @Override
    public Object getItem(int position) {
        return utlAndTitle.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = layoutInflater.inflate(R.layout.item_grid_view, parent, false);
        }
        TextView name = ViewHolder.getHolderView(convertView, R.id.tv_item);
        ImageView img = ViewHolder.getHolderView(convertView, R.id.iv_item);
        initData(name, img, position);
        return convertView;
    }

    private void initData(TextView name, ImageView img, int position) {
        String type = utlAndTitle.get(position).split(",")[1];
        String url = utlAndTitle.get(position).split(",")[0];
        name.setText(type);
        ImageLoaderUtil.getImageView(url, img);
    }
}
